package com.sd.view;

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alex.books.BaseBook;
import com.sd.activity.R;
import com.sd.app.AppInit;
import com.sd.util.AlexStringUtil;

public class BookListAdapter extends BaseAdapter {

	private Vector<BaseBook> mBooks;
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	
	public BookListAdapter(Context context,Vector<BaseBook> books){
		mContext = context;
		mBooks = books;
		mLayoutInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mBooks.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mBooks.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.bookview2, null);
			holder.book1 = (ImageView)convertView.findViewById(R.id.firstline);
			holder.text = (ImageView)convertView.findViewById(R.id.text);
			holder.bookName = (TextView)convertView.findViewById(R.id.bookname);
			holder.bookreadInfo = (TextView)convertView.findViewById(R.id.bookreadinfo);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		if(position==0){
			holder.book1.setVisibility(View.VISIBLE);
		}else{
			holder.book1.setVisibility(View.GONE);
		}
		
		if(AlexStringUtil.isUnknownString(mBooks.get(position).mBookCover)){
			holder.text.setBackgroundResource(R.drawable.testbook_bg2);
		}else{
			holder.text.setBackgroundDrawable(new BitmapDrawable(AppInit.getInstance().APP_BOOKS + mBooks.get(position).mBookCover));
		}
		
		holder.bookName.setText(mBooks.get(position).mBookName);
		holder.bookreadInfo.setText(mBooks.get(position).mReaderingInfo.mCurChapter.mChapterName);
		
		return convertView;
	}
	
	class ViewHolder {
		/**
		 * 做法名字
		 */
		ImageView book1;
		ImageView text;
		/**
		 * 做法介绍
		 */
		TextView bookName;
		/**
		 * 做法加价
		 */
		TextView bookreadInfo;
	}
	
	
}
